/**
 * 
 */
package br.com.rodolfo.timesheet.dao;

import java.sql.SQLException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.com.rodolfo.timesheet.entity.Cargo;

/**
 * @author rjulio
 *
 */
@Repository("cargoDAO")
public class CargoDAO {
	
	@PersistenceContext
	protected EntityManager entityManager;

	@Transactional(readOnly=true)
	public List<Cargo> findAll() 
			throws SQLException{
		String sQuery = "Select c from Cargo ";
		Query q = entityManager.createQuery(sQuery);
		@SuppressWarnings("unchecked")
		List<Cargo> list = (List<Cargo>) q.getResultList();
		return list;
	}
	
	@Transactional(readOnly=true)
	public Cargo findByName(String name) 
			throws SQLException{
		String sQuery = "Select top(1) c from Cargo where c.descricao = :descricao";
		Query q = entityManager.createQuery(sQuery);
		q.setParameter("descricao", name);
		return (Cargo) q.getSingleResult();
		
	}



}
